<template>
  <div class="app-container dict-actLoanApprovalList-list-wrap">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
      <el-form-item label="所属分行" prop="deptId">
        <treeselect v-model="queryParams.deptId" :options="deptList" :normalizer="normalizer" :show-count="true"
          :default-expand-level="1" placeholder="请选择" style="width: 215px;" />
      </el-form-item>
      <el-form-item label="产品" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="" clearable />
      </el-form-item>
      <el-form-item label="信审人员" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="" clearable />
      </el-form-item>
      <el-form-item label="审批额度" prop="approvalMoneyBegin">
        <el-input v-model="queryParams.approvalMoneyBegin" placeholder="" clearable style="width: 100px;" />-
        <el-input v-model="queryParams.approvalMoneyEnd" placeholder="" clearable style="width: 100px;" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>

      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="clickAddbtn"
          v-hasPermi="['custom:loanApproval:add']">添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple" size="mini" @click="handleDelete"
          v-hasPermi="['config:loanApproval:del']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary" 
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="dialogVisible=true" >批量修改</el-button>
      </el-col>   -->
    </el-row>
    <el-table v-loading="loading" max-height="550" :data="dataList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handelEdit(scope.row)" v-hasPermi="['custom:loanApproval:edit']">编辑
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="产品" prop="productName" />
      <el-table-column label="所属分行" prop="deptName"></el-table-column>
      <el-table-column label="信审人员" prop="nickName" />
      <el-table-column label="审批额度" prop="approvalMoney" />
      <el-table-column label="创建日期" prop="createTime" width="160px" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-dialog :title="ruleForm.id?'编辑审批额度':'添加审批额度'" :visible.sync="dialogVisible" width="800px">
      <div class="dialog-form-box">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
          <el-form-item label="所属分行：" prop="deptId">
            <treeselect :multiple="ruleForm.id?false:true" v-model="ruleForm.deptId" :options="deptList"
              :normalizer="normalizer" :show-count="true" placeholder="请选择" :default-expand-level="1"
              value-consists-of="ALL" @close="selectDept" style="width: 85%;" />
          </el-form-item>
          <el-form-item label="产品名称：" prop="productId" v-if="ruleForm.id">
            <el-select v-model="ruleForm.productId" placeholder="请选择" clearable style="width: 85%;">
              <el-option v-for="item in productList" :label="item.label" :value="item.key" />
            </el-select>
          </el-form-item>
          <el-form-item label="产品选择：" prop="productIds" v-if="!ruleForm.id">
            <!-- <span>分行下的产品选择：</span> -->
            <el-transfer v-model="ruleForm.productIds" :titles="['未选产品', '已选产品']" :data="productList">
              <span slot-scope="{ option }">{{ option.productName }}</span>
            </el-transfer>
          </el-form-item>
          <el-form-item label="可审批额度：" prop="approvalMoney">
            <el-input v-model="ruleForm.approvalMoney" style="width: 85%;"></el-input>
          </el-form-item>
          <el-form-item label="审核人员：" prop="userId">
            <treeselect :multiple="ruleForm.id?false:true" :flat="true" v-model="ruleForm.userId" :options="usersList"
              :show-count="true" :default-expand-level="1" placeholder="请选择" style="width: 85%;">
              <label slot="option-label" slot-scope="{ node, shouldShowCount, count, labelClassName, countClassName }">
                <i class="el-icon-user-solid" v-if="!node.isDisabled"></i> {{node.label }}
                <span v-if="shouldShowCount" :class="countClassName">({{ count }})</span>
              </label>
            </treeselect>
          </el-form-item>

        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handelConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import {
    loanApprovalList,
    loanApprovalAdd,
    loanApprovalEdit,
    loanApprovalDelBatch,
    getProductByOffice,
    deptUserTree
  } from '@/api/system/dict/actLoanApprovalList.js'
  import {
    listDept
  } from "@/api/system/dept";

  export default {
    name: "DictActLoanApprovalList",
    components: {
      Treeselect
    },
    data() {
      return {
        loading: false,
        multiple: true,
        queryParams: {
          pageNum: 1,
          pageSize: 30
        },
        dialogVisible: false,
        ruleForm: {},
        rules: {
          deptId: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
          productId: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
          productIds: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
          approvalMoney: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
          userId: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
        },
        dataList: [],
        total: 0,
        deptList: [],
        productList: [],
        usersList: [],

      };
    },


    created() {
      this.getList()
      this.listDept()
      this.deptUserTree()

    },
    methods: {
      /** 转换菜单数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.deptId,
          label: node.deptName,
          children: node.children
        };
      },
      userNormalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.label,
          isDisabled: node.type == 'dept' ? true : false,
          children: node.children
        };
      },
      selectDept(val, data) {
        console.log(val)
        this.getProductByOffice()
      },
      //树状结构user查询
      deptUserTree() {
        deptUserTree().then(res => {
          this.usersList = res.data
        })
      },
      //获取所属分行
      listDept() {
        listDept().then(response => {
          this.deptList = this.handleTree(response.data, "deptId");
        });
      },
      //查询所选公司下的产品
      getProductByOffice() {
        let deptIds = ''
        if (this.ruleForm.deptId.length > 0) {
          this.ruleForm.deptId.forEach((item, i) => {
            if (this.ruleForm.deptId.length - 1 == i) {
              deptIds += item
            } else {
              deptIds += item + ','
            }
          })
        }

        getProductByOffice({
          deptId: deptIds
        }).then(res => {
          res.data.forEach(item => {
            item.key = item.id
            item.label = item.productName
          })
          this.productList = res.data
        })
      },
      getList() {
        this.loading = true;
        loanApprovalList(this.queryParams).then(res => {
          this.dataList = res.rows
          this.total = res.total
          this.loading = false;
        })
      },
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      resetQuery() {
        this.resetForm("queryForm");
        this.queryParams.approvalMoneyEnd = ''
        this.handleQuery();

      },
      handleSelectionChange(selection) {
        this.seletIds = selection.map(item => item.id)
        this.multiple = !selection.length
      },
      //批量删除产品
      handleDelete() {
        let that = this
        let ids = ''
        this.seletIds.forEach((item, i) => {
          if (this.seletIds.length - 1 == i) {
            ids += item
          } else {
            ids += item + ','
          }
        });
        this.$modal.confirm('是否确认删除所选数据项？').then(function () {
          loanApprovalDelBatch({
            ids: ids
          }).then(res => {
            that.getList();
            that.$modal.msgSuccess("删除成功");
          });
        }).catch(function () {});
      },
      // 表单重置
      reset() {
        this.resetForm("ruleForm");
        this.ruleForm = {
          id: '',
          deptId: null,
          approvalMoney: '',
          userId: null,
          productId: '',
          productIds: []
        };
      },
      //点击添加按钮弹出表单
      clickAddbtn() {
        this.dialogVisible = true
        this.reset()
      },
      //点击编辑弹出编辑表单并赋值
      handelEdit(row) {
        this.dialogVisible = true
        this.reset()
        this.ruleForm.id = row.id
        this.ruleForm.deptId = [row.deptId]
        this.ruleForm.approvalMoney = row.approvalMoney
        this.ruleForm.userId = [row.userId]
        this.ruleForm.productId = row.productId
        this.ruleForm.productIds = [row.productId]
        this.getProductByOffice()
      },
      //点击提交表单
      handelConfirm() {
        let deptIds = ''
        if (this.ruleForm.deptId && this.ruleForm.deptId.length > 0) {
          this.ruleForm.deptId.forEach((item, i) => {
            if (this.ruleForm.deptId.length - 1 == i) {
              deptIds += item
            } else {
              deptIds += item + ','
            }
          })
        }
        let productIds = ''
        if (this.ruleForm.productIds && this.ruleForm.productIds.length > 0) {
          this.ruleForm.productIds.forEach((item, i) => {
            if (this.ruleForm.productIds.length - 1 == i) {
              productIds += item
            } else {
              productIds += item + ','
            }
          })
        }


        let userIds = ''
        if (this.ruleForm.userId && this.ruleForm.userId.length > 0) {
          this.ruleForm.userId.forEach((item, i) => {
            if (this.ruleForm.userId.length - 1 == i) {
              userIds += item
            } else {
              userIds += item + ','
            }
          })
        }
        this.$refs["ruleForm"].validate(valid => {
          if (valid) {
            const params = {
              id: this.ruleForm.id || null,
              deptId: deptIds,
              approvalMoney: this.ruleForm.approvalMoney,
              userId: userIds,
              productId: this.ruleForm.id ? this.ruleForm.productId : productIds
            }
            if (this.ruleForm.id) {
              loanApprovalEdit(params).then(res => {
                this.$modal.msgSuccess("修改成功");
                this.dialogVisible = false;
                this.getList();
              })
            } else {
              loanApprovalAdd(params).then(res => {
                this.$modal.msgSuccess("添加成功");
                this.dialogVisible = false;
                this.getList();
              })
            }
          }
        })
      }
    }
  };

</script>

<style rel="stylesheet/scss" lang="scss">

</style>
